# encoding: UTF-8
'''
Created on 2013-9-7

@author: Ben

游戏中的线程父类
当连接断开或主线程结束时不再发包
'''

import ThreadManager
import threading
import time

#暂停状态
THREAD_STATE_PAUSE = 1
#运行状态
THREAD_STATE_RUNNING = 2
#死亡状态
THREAD_STATE_DEAD = 3

class BasicThread(threading.Thread):
    '''
    classdocs
    '''
    
    #线程状态
    state = THREAD_STATE_PAUSE

    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        threading.Thread.__init__(self, group, target, name, args, kwargs)
        ThreadManager.addThread(self)
        
    '''
    启动线程
    '''
    def startThread(self):
        if not self.isAlive():
            self.start()
        self.state = THREAD_STATE_RUNNING
    
    '''
    暂停线程
    '''
    def pauseThread(self):
        self.state = THREAD_STATE_PAUSE
    
    '''
    杀敌线程
    '''
    def killThread(self):
        self.state = THREAD_STATE_DEAD
    
    '''
    线程入口
    '''
    def run(self):
        while self.state != THREAD_STATE_DEAD:
            if self.state == THREAD_STATE_RUNNING:
                self.callback()
            else:
                time.sleep(1)
        
    '''
    线程回调函数
    '''
    def callback(self):
        time.sleep(1)